package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.ui.DropShadowBorder;
import ac.svn.accesscontrol.ui.IUIConstants;
import ac.svn.accesscontrol.ui.JPanel;
import ac.svn.accesscontrol.ui.JScrollPane;
import ac.svn.accesscontrol.ui.MarginBorder;
import ac.svn.accesscontrol.ui.TitledBorder;
import ac.svn.accesscontrol.ui.UIComponentLibrary;
import ac.svn.accesscontrol.ui.XMenuBar;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;
import ac.svn.accesscontrol.ui.panel.RToolbar;
import ac.svn.accesscontrol.ui.renderer.AccessListCellRenderer;
import ac.svn.accesscontrol.ui.renderer.LocationListCellRenderer;
import ac.svn.accesscontrol.ui.renderer.UserTreeCellRenderer;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class MainWindow extends JFrame implements IUIConstants 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private XMenuBar menuBar;
	private JToolBar statusBar;
	private JLabel statusLabel;
	private JLabel errorLabel;
	private RToolbar toolBar;
	private JPanel basePanel;
	private JList locationListView;
	private ThreadedListModel locationListModel;
	private JList accessListView;
	private ThreadedListModel  accessListModel;
	private JScrollPane treeScrollPane;
	private JTree svnTree;
	private DefaultMutableTreeNode rootNode;
	private JTextField userSearchTxt;
	private JTextField locationSearchTxt;
	private JSplitPane splitPane;
	private JScrollPane scrpLocations; 
	

	//private JTable tableView;

	public MainWindow()
	{
		super("Smart CM");
		initComponents();
		registerComponents();
		setSize(600,400);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
	}

	private void registerComponents() {
		UIComponentLibrary.addComponent(C_MAIN_WINDOW, this);
		UIComponentLibrary.addComponent(C_MENU_BAR, menuBar);
		UIComponentLibrary.addComponent(C_STATUS_BAR, statusBar);
		UIComponentLibrary.addComponent(C_STATUS_LABEL, statusLabel);
		UIComponentLibrary.addComponent(C_ERROR_LABEL,errorLabel);
		UIComponentLibrary.addComponent(C_TOOL_BAR, toolBar);
		UIComponentLibrary.addComponent(C_LOCATION_MODEL,locationListModel);
		UIComponentLibrary.addComponent(C_LOCATION_LIST,locationListView);
		UIComponentLibrary.addComponent(C_ACCESS_MODEL,accessListModel);
		UIComponentLibrary.addComponent(C_ACCESS_LIST,accessListView);
		UIComponentLibrary.addComponent(C_USERS_TREE,svnTree);
		UIComponentLibrary.addComponent(C_ROOT_NODE,rootNode);
		UIComponentLibrary.addComponent(C_TEXT_S_LOCATION,locationSearchTxt);
		UIComponentLibrary.addComponent(C_TEXT_S_USER,userSearchTxt);
		UIComponentLibrary.addComponent(C_LOCATION_LIST_VIEW_HOLDER,splitPane);
	}

	private void initComponents() {
		getContentPane().setLayout(new BorderLayout(5,0));

		menuBar = new XMenuBar();
		setJMenuBar(menuBar);
		setBackground(Color.WHITE);

		statusLabel = new JLabel("Welcome to Smart CM");
		errorLabel = new JLabel("Issues: 0");
		errorLabel.setForeground(Color.RED);

		statusBar = new JToolBar();
		statusBar.add(statusLabel);
		statusBar.addSeparator();
		statusBar.add(errorLabel);
		statusBar.addSeparator();
		statusBar.setFloatable(false);
		statusBar.setBackground(Color.WHITE);
		statusBar.setPreferredSize(new Dimension(300,40));
		UIUtility.setAllShadowedBorder(statusBar);

		getContentPane().add(statusBar,BorderLayout.SOUTH);

		toolBar = new RToolbar();
		toolBar.setFloatable(false);

		//JScrollPane tsp = new JScrollPane(toolBar);
		getContentPane().add(toolBar,BorderLayout.NORTH);

		basePanel = new JPanel(new BorderLayout(5, 5),true);
		//UIUtility.setAllShadowedBorder(basePanel);

		rootNode = new DefaultMutableTreeNode("Users");
		svnTree = new JTree(rootNode);
		svnTree.setDragEnabled(true);

		svnTree.setBorder(UIManager.getBorder("TextField.border"));
		svnTree.setCellRenderer(new UserTreeCellRenderer());
		svnTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
		ToolTipManager.sharedInstance().registerComponent(svnTree);
		treeScrollPane = new JScrollPane(svnTree);
		treeScrollPane.setPreferredSize(new Dimension(300, 700));
		treeScrollPane.setBorder(new TitledBorder("Groups  [Search: F3]"));
		treeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		basePanel.add(treeScrollPane,BorderLayout.WEST);
		//svnTree.setScrollsOnExpand(true);

		JScrollPane scrollPane;

		new JPanel((new BorderLayout(5, 5)),true);
		splitPane 	 	= new JSplitPane(JSplitPane.VERTICAL_SPLIT);

		splitPane.setBackground(Color.WHITE);
		splitPane.setBorder(new TitledBorder("SVN"));

		locationListModel = new ThreadedListModel();
		locationListView = new JList(locationListModel);
		locationListView.setBorder(UIManager.getBorder("TextField.border"));

		//SimpleIconRenderer iconRenderer = new SimpleIconRenderer(IResources.LOC_ICN_URL);
		//iconRenderer.setTextFormatter(new LocationItemNameFormatter());
		locationListView.setCellRenderer(new LocationListCellRenderer());

		scrpLocations = new JScrollPane(locationListView);
		scrpLocations.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrpLocations.setBorder(new TitledBorder("Locations [Search: F4]"));
		scrpLocations.setPreferredSize(new Dimension(500, 300));
		//bodyPanel.add(scrollPane,BorderLayout.CENTER);
		splitPane.setTopComponent(scrpLocations);

		accessListModel = new ThreadedListModel();
		accessListView  = new JList(accessListModel);
		accessListView.setCellRenderer(new AccessListCellRenderer());
		scrollPane = new JScrollPane(accessListView);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBorder(new TitledBorder("Rights"));
		scrollPane.setPreferredSize(new Dimension(500, 200));
		//bodyPanel.add(scrollPane,BorderLayout.SOUTH);
		splitPane.setBottomComponent(scrollPane);

		splitPane.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				JSplitPane s = (JSplitPane) arg0.getSource();
				s.setDividerLocation(.75);
			}
		});

		userSearchTxt = new JTextField();
		userSearchTxt.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				JComponent component = (JComponent) e.getSource();
				component.setBorder((new JTextField()).getBorder());
			}

			@Override
			public void focusGained(FocusEvent e) {
				JComponent component = (JComponent) e.getSource();
				component.setBorder(new DropShadowBorder(Color.GREEN,5,0.7f,7,true,true,true,true));
			}
		});
		locationSearchTxt = new JTextField();
		locationSearchTxt.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				JComponent component = (JComponent) e.getSource();
				component.setBorder((new JTextField()).getBorder());
			}

			@Override
			public void focusGained(FocusEvent e) {
				JComponent component = (JComponent) e.getSource();
				component.setBorder(new DropShadowBorder(Color.GREEN,5,0.7f,7,true,true,true,true));
			}
		});

		addUserSearchComponent(userSearchTxt);
		addLocationSearchComponent(locationSearchTxt);

		//bodyPanel.add(splitPane,BorderLayout.CENTER);
		basePanel.add(splitPane,BorderLayout.CENTER);


		getContentPane().add(basePanel,BorderLayout.CENTER);
	}

	public void setLocation() {
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(size.getWidth()/2 - getWidth()/2),(int)(size.getHeight()/2-getHeight()/2));
	}

	public void addLocationSearchComponent(Component component)
	{
		JPanel searchPanel = new JPanel((new BorderLayout(5,2)),true);
		searchPanel.setBorder(new MarginBorder());
		JLabel searchLabel = new JLabel(Utility.getImageIcon(IResources.LOC_ICN_SEARCH));
		searchLabel.setText("Search");
		searchPanel.add(searchLabel,BorderLayout.WEST); 
		searchPanel.add(component,BorderLayout.CENTER);
		scrpLocations.setColumnHeaderView(searchPanel);
	}

	public void addUserSearchComponent(Component component)
	{
		JPanel searchPanel = new JPanel((new BorderLayout(5,2)),true);
		searchPanel.setBorder(new MarginBorder());
		JLabel searchLabel = new JLabel(Utility.getImageIcon(IResources.LOC_ICN_SEARCH));
		searchLabel.setText("Search");
		searchPanel.add(searchLabel,BorderLayout.WEST); 
		searchPanel.add(component,BorderLayout.CENTER);
		treeScrollPane.setColumnHeaderView(searchPanel);
	}

	public static void main(String args[]) throws Exception
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		MainWindow mainWindow = new MainWindow();
		mainWindow.setVisible(true);
	}   

	public void addLocationView(Component component)
	{
		if(null != splitPane)
		{
			basePanel.add(component,BorderLayout.CENTER);
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					SwingUtilities.updateComponentTreeUI(basePanel);
				}
			});
		}
	}    

	public JScrollPane getLocationListScroller()
	{
		return scrpLocations;
	}
	
	

	@Override
	public void setVisible(boolean arg0) {
		super.setVisible(arg0);
	}

	/**
	 * @return the locationListView
	 */
	public JList getLocationListView() {
		return locationListView;
	}

	/**
	 * @return the accessListView
	 */
	public JList getAccessListView() {
		return accessListView;
	}

	/**
	 * @return the svnTree
	 */
	public JTree getSvnTree() {
		return svnTree;
	}

	/**
	 * @param svnTree the svnTree to set
	 */
	public void setSvnTree(JTree svnTree) {
		this.svnTree = svnTree;
	}


}
